package com.example.demo.exceptions;

import com.example.demo.commons.Commons;
import com.example.demo.http.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpRequest;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.method.annotation.MethodArgumentTypeMismatchException;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
@Slf4j
public class GlobalException {


    @ExceptionHandler(Exception.class)
    @ResponseBody
    public Object defalutException(HttpServletRequest request, Exception e){
        log.error("url:"+request.getRequestURI()+",发送全局异常："+e.getLocalizedMessage());
        return Result.createError(Commons.NET_DEFALUT_EXCEPTION,Commons.NET_DEFALUT_EXCEPTION_MSG);
    }

    @ExceptionHandler(MethodArgumentTypeMismatchException.class)
    @ResponseBody
    public Object MethodArgumentTypeMismatchException(HttpServletRequest request, Exception e){
        log.error("url:"+request.getRequestURI()+",局部异常："+e.getLocalizedMessage());
        return Result.createError(Commons.NET_EXCEPTION_METHODARGUMENTYPEMISMATH,Commons.NET_EXCEPTION_METHODARGUMENTYPEMISMATH_MSG);
    }
}
